<?php
namespace Application\Api\Controller;

class Project extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_PROJECT);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    private function setStatus($data)
    {
        if(isset($data['dry_status']) && $data['dry_status'] == 'on'){
            $data['dry_status'] = 1;
        }
        else{
            $data['dry_status'] = 0;
        }
        return $data;
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $data['dry_user'] = $userId;
        $data['dry_data_sort'] = 0;
        $data['dry_sort'] = isset($data['dry_sort']) ? $data['dry_sort'] : 0;
        $data['dry_status'] = isset($data['dry_status']) ? $data['dry_status'] : 1;
        $id = $service->add($data);
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function deleteAction(){
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        $rows = $service->delete($id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function getListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        $temp = $this->getService()->getList($userId, $page, $limit);
        $list = $this->get(SERVICE_OSSFILE)->getFileData($temp['list'], ['dry_image' => 2]);
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
            'count' => $temp['count']
        ];
        $this->sendSuccess($data);
    }

    public function getListByIdAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $id = $data['id'];
        $temp = $this->getService()->one($id);
        $list = $this->get(SERVICE_OSSFILE)->getFileData($temp['list'], ['dry_image' => 1]);
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
        ];
        $this->sendSuccess($data);
    }

    /*
        /Api/Project/getUploadData
    */
    public function getUploadDataAction()
    {
        $data = $this->get(SERVICE_OSS)->getUploadData('spwork');
        $this->sendSuccess($data);
    }

}